<template>
<div class="publish-container">
    <el-card class="box-card">
    <div slot="header" class="clearfix">
         <Breadcrumb>内容管理</Breadcrumb>
    </div>
    <el-form :model="form" :rules="rules"  ref="form" label-width="80px" class="demo-ruleForm">
      <el-form-item label="标题" prop="title"  >
        <el-input v-model="form.title" autocomplete="off"></el-input>
      </el-form-item>
       <el-form-item label="内容" prop="content">
         <quill-editor v-model="form.content" :options="editorOption"></quill-editor>
        </el-form-item>
      <el-form-item label="封面">
        <el-radio-group  v-model="form.coverType" @change="hReSetImageList">
          <el-radio :label="1">单图</el-radio>
          <el-radio :label="3">三图</el-radio>
          <el-radio :label="0">无图</el-radio>
          <el-radio :label="-1">自动</el-radio>
        </el-radio-group>
      </el-form-item>
        <el-form-item label="" v-if="form.coverType > 0" >
          <!--
            el-row 行 el-col 列
           -->
          <el-row>
            <el-col :span="6" v-for="(item,idx) in form.coverType" :key="idx">
              <!-- {{idx}} -->
              <Cover v-model="form.coverImages[idx]"/>
            </el-col>
          </el-row>
      </el-form-item>
      <Channel v-model="form.channelId" />
     <el-form-item>
        <el-button type="primary" @click="hSave"  v-loading="loading">保存</el-button>
        <!-- <el-button @click="hAdd(true)">存入草稿</el-button> -->
     </el-form-item>
    </el-form>
  </el-card>
</div>
</template>

<script>
// 导入样式
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import Channel from '@/components/Channel.vue'
// 导入组件
import { quillEditor } from 'vue-quill-editor'
export default {
  name: 'AddArticle',
  components: {
    quillEditor,
    Channel
  },
  data () {
    return {
      editorOption: {
        // 对富文本编辑器的配置
        // 占位配置
        placeholder: '',
        modules: {
          // 配置工具栏
          toolbar: [
            ['bold', 'italic', 'underline', 'strike'],
            ['blockquote', 'code-block'],
            [{ header: 1 }, { header: 2 }],
            [{ list: 'ordered' }, { list: 'bullet' }],
            [{ indent: '-1' }, { indent: '+1' }],
            ['image']
          ]
        }
      },
      form: {
        // 这个是点击的哪个参数的id
        id: '',
        title: '', // 文章
        content: '', // 内容
        coverType: 0, // 封面类型：-1（自动）， 0（无图）， 1（1张图）， 3（3张图）
        // 当前选中频道
        channelId: '',
        // 保存对应的封面图的地址
        coverImages: []

      },
      loading: false,
      rules: {
        title: [
          { required: true, message: '请输入标题', trigger: 'blur' },
          { min: 5, max: 30, message: '长度在 5 到 30 个字符', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '请输入内容', trigger: 'blur' }
        ],
        // coverType: [
        //   { type: 'array', required: true, message: '请选择一张封面图', trigger: 'change' }
        // ],
        channelId: [
          { required: true, message: '请选择所属频道', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.loadDetail()
  },
  methods: {
    // 保存文章
    async loadDetail () {
      const res = await this.$http({
        method: 'GET',
        url: '/mp/v1_0/articles/' + this.$route.params.id
      })
      this.form.title = res.data.data.title
      this.form.content = res.data.data.content
      this.form.coverType = res.data.data.cover.type
      this.form.coverImages = res.data.data.cover.images
      this.form.channelId = res.data.data.channel_id
      this.form.id = res.data.data.id
      // console.log('年后啊', res)
    },
    // 选择不同对面类型，需要重置一下图片数组
    hReSetImageList () {
      // 0,1,-1   =====> 3
      if (this.form.coverType === 3) {
      } else if (this.form.coverType === 1) {
        //  3 === > 1
        // this.form.coverImages.splice(1)
        this.form.coverImages = [this.form.coverImages[0]]
      }
    },
    // 发表
    async hSave () {
      this.$refs.form.validate(result => {
        // 用户点击了保存草稿
        // 1. 收集文章信息
        // console.log(this.form)
        // 2. 验证数据- el-form表单
        if (result) {
          this.doSave()
        }
      })
    },
    async doSave () {
      this.loading = true
      try {
        // 3.调用接口
        const res = await this.$http({
          method: 'PUT',
          url: '/mp/v1_0/articles/' + this.form.id,
          data: {
            title: this.form.title,
            content: this.form.content,
            cover: {
              type: this.form.coverType,
              images: this.form.coverImages
            },
            channel_id: this.form.channelId

          }
        })
        this.loading = false
        // 1.提示用户ok
        this.$message.success('发布文章成功！')
        // 2. 清空表单
        this.form.title = ''
        //  跳转内容管理
        this.$router.push('/article')
        console.log(res)
      } catch (err) {
        console.log('这个是错误', err)
        this.loading = false
        this.$message.error('发布文章失败！')
      }
    },
    resetForm () {
      console.log('11111')
    }
  }
}
</script>
<style scoped lang='less'>
// 在父组件中去设置子组件的样式，加上/deep/ 实现穿透功能
.publish-container /deep/ .ql-editor{
  height: 300px;
}
.publish-container /deep/ .ql-toolbar.ql-snow{
  padding: 0 8px;
  .boke{
    width: 240px;
    height: 240px;
    background-color: red;
    .tu1{
        background: url("../../assets/default.png") no-repeat ;
    }
  }
}
.el-col-6{
  width: 15%;
}

</style>
